package com.wmlive.hhvideo.heihei.login.presenter;

import android.text.TextUtils;

import com.wmlive.hhvideo.common.base.BasePresenter;
import com.wmlive.hhvideo.common.network.HttpConstant;
import com.wmlive.hhvideo.heihei.beans.login.MobileLoginResponse;
import com.wmlive.hhvideo.heihei.beans.splash.InitCatchData;
import com.wmlive.hhvideo.heihei.login.AccountUtil;
import com.wmlive.hhvideo.heihei.login.view.MobileLoginView;
import com.wmlive.networklib.entity.DCNetObserver;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by vhawk on 2017/5/23.
 */

public class MobileLoginPresenter extends BasePresenter<MobileLoginView> {


    public MobileLoginPresenter(MobileLoginView view) {
        super(view);
    }

    public void mobileLogin(String mobile, String smsCode) {
        if (!TextUtils.isEmpty(InitCatchData.userSignInCLByPhone())) {
            executeRequest(HttpConstant.TYPE_MOBILE_LOGIN, getHttpApi().mobileLogin(InitCatchData.userSignInCLByPhone(), mobile, smsCode))
                    .subscribe(new DCNetObserver<MobileLoginResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, final MobileLoginResponse response) {
                            if (null != viewCallback) {
                                if (response != null && !TextUtils.isEmpty(response.getToken())) {
                                    AccountUtil.resetAccount(response);
                                    viewCallback.handleLoginSucceed();
                                    Observable.just(1)
                                            .subscribeOn(Schedulers.io())
                                            .map(new Function<Integer, Boolean>() {
                                                @Override
                                                public Boolean apply(Integer integer) throws Exception {
                                                    AccountUtil.updateUserInfo(response);
                                                    return true;
                                                }
                                            })
                                            .observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(new Consumer<Boolean>() {
                                                @Override
                                                public void accept(Boolean aBoolean) throws Exception {
                                                    if (viewCallback != null) {
                                                        viewCallback.handleLoginComplete();
                                                    }
                                                }
                                            }, new Consumer<Throwable>() {
                                                @Override
                                                public void accept(Throwable throwable) throws Exception {
                                                    if (viewCallback != null){
                                                        viewCallback.handleLoginError();
                                                    }
                                                }
                                            });
                                } else {
                                    if (viewCallback != null){
                                        viewCallback.handleLoginError();
                                    }
                                }
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (null != viewCallback) {
                                viewCallback.handleLoginFailure(message);
                            }
                        }
                    });
        } else {
            if (null != viewCallback) {
                viewCallback.handleNetError();
            }
        }

    }


}
